package cn.vcjf.weapp.api.user.controller;


import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.web.bind.annotation.ResponseBody;
import cn.vcjf.weapp.common.web.TableResult;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import cn.vcjf.weapp.api.user.entity.UserComment;
import cn.vcjf.weapp.api.user.service.UserCommentService;

import org.springframework.stereotype.Controller;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhengzihang
 * @since 2018-09-25
 */
@Controller
@RequestMapping("/user/userComment")
public class UserCommentController {
	@Resource
	private UserCommentService userCommentService;

	/**
	 * 添加商品的评论
	 *
	 * @param userComment
	 * @return
	 */
	@RequestMapping("/addUserComment")
	@ResponseBody
	public TableResult addUserComment(@Valid UserComment userComment, BindingResult result) {

		TableResult.checkParams(result);
		userComment.preInsert();
		boolean b = userCommentService.insert(userComment);
		return TableResult.Result(b);
	}

	@RequestMapping("/updateUserComment")
	@ResponseBody
	public TableResult updateUserComment(UserComment userComment) {
		userComment.preUpdate();
		boolean b = userCommentService.updateById(userComment);
		return TableResult.Result(b);
	}

	@RequestMapping("/findUserCommentOne")
	@ResponseBody
	public TableResult findUserCommentOne(UserComment userComment) {

		EntityWrapper<UserComment> wrapper = new EntityWrapper<UserComment>();

		wrapper.eq("id", userComment.getId());

		UserComment result = userCommentService.selectOne(wrapper);

		return TableResult.OKResult(result);
	}

	/**
	 * 商品评论列表
	 * @param userComment
	 * @return
	 */
	@RequestMapping("/findUserCommentList")
	@ResponseBody
	public TableResult findUserCommentList(UserComment userComment) {

		EntityWrapper<UserComment> wrapper = new EntityWrapper<UserComment>();
		wrapper.eq("uc.sysUserId", userComment.getSysUserId());
		wrapper.eq("uc.goodsId", userComment.getGoodsId());
		//晒图
		if(StringUtils.isNotBlank(userComment.getCommentFiles())){
			wrapper.ne("uc.commentFiles", "");
			wrapper.isNotNull("uc.commentFiles");
		}

		Page<UserComment> page = userComment.getPageInfo();

		page = userCommentService.selectPage(page, wrapper);

		return TableResult.OKResult(page);

	}

	@RequestMapping("/deleteUserComment")
	@ResponseBody
	public TableResult deleteUserComment(UserComment userComment) {
		userComment.preUpdate();
		boolean b = userCommentService.deleteById(userComment.getId());
		return TableResult.Result(b);
	}

}
